<?php
class UsercenterModel extends Model {
	protected $tableName = 'usercenter';
	protected $_validate = array(
		//验证用户名注册
		array('username' , '4,32', -11 ,   EXISTS_VALIDATE , 'length'), //验证长度
		array('username' , 'unique', -12 , EXISTS_VALIDATE , 'unique'), //验证唯一性
		array('username' , 'checkUserName' ,    -13 , EXISTS_VALIDATE , 'callback'),//回调验证用户名合法
		array('username' , 'checkDenyUserName' ,-14 , EXISTS_VALIDATE , 'callback'),//回调验证用户名是否被

		//验证用户密码
		array('password' , '4,30' , -41 , EXISTS_VALIDATE , 'length'), //验证密码长度  
	);
	protected $_auto = array(
		array('password', 'md5', self::MODEL_BOTH, 'function'),
		array('reg_time', NOW_TIME , self::MODEL_INSERT ),
		array('reg_ip'  , 'get_client_ip', self::MODEL_INSERT , 'function' , 1),
		array('update_time' , NOW_TIME),
		array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
	);

	/**
	 * 用户中心新用户
	 * @param  string $username [用户名]
	 * @param  string $nikename [昵称]
	 * @param  string $password [密码]
	 * @param  string $mobile   [电话]
	 * @return integer          [返回值]
	 */
	public function register($username,$nikename,$password,$email ,$mobile ,$type = 1) {
		//组合数据
		$data = array(
			'username' => $username,
			'nikename' => $nikename,
			'password' => $password,
			'email'    => $email,
			'mobile'   => $mobile,
			'reg_type' => $type,
		);

		//数据过滤
		if(empty($nikename)) {
			unset($data['nikename']);
		}
		if(empty($email)) {
			unset($data['email']);
		}
		if(empty($mobile)) {
			unset($data['mobile']);
		}
		//创建数据
		$UserCenterMember = $this->create($data);
		if($UserCenterMember) {
			//向会员表插入一条数据
			$res = true;
			if($res) {
				//添加一条用户中心数据
				$uid = $this->add($UserCenterMember);
				if($uid === false) {
					//回溯会员表数据
				}
				return  $uid ? $uid : false;
			}else{
				return $ress;
			}
		}else{
			return $this->getError(); //错误详情见自动验证注释
		}

	}
	
	/**
	 * 用户中心登录
	 * @param  [type] $username [description]
	 * @param  [type] $password [description]
	 * @param  [type] $regType  [description]
	 * @return [type]           [description]
	 */
	public function login($username , $password , $regType) {
		//1.组合查询参数
		$map = array();
		switch ($regType) {
			case 1:
				$map['username'] = $username;
				break;
			case 2:
				$map['email']    = $username;
				break;
			case 3:
				$map['mobile']   = $username;
				break;
			default:
				return false;
				break;
		}

		//2. 查询用户是否存在
		$user = $this->where($map)->find();

		if(is_array($user) && $user['status']) {
			//验证用户密码
			if( (md5($password)) == $user['password']) {
				//更新登录信息
				
				$this->updateLogin($user['id']);
				return $user['id'];
			}else{
				//密码错误
				return -2;
			}
		}else{
			//用户不存在 或者 禁用
			return -1;
		}
	}

	/**
     * 更新用户登录信息
     * @param  integer $uid 用户ID
     */
    protected function updateLogin($userId)
    {
        $data = array(
			'id'              => $userId,
			'last_login_time' => NOW_TIME,
			'last_login_ip'   => get_client_ip(1),
        );
        $this->save($data);
    }

	/**
	 * 验证用户名是否合法
	 * @param  string $username [用户名]
	 * @return integer          [返回值]
	 */
	public function checkUserName($username) {
		//1.检查是否有空格
		if(strpos($username, ' ') !== false) {
			return false;
		}
		//2.正则匹配是否合法
		preg_match('/^[a-zA-Z0-9_]{4,32}$/', $username , $result);
		if(!$result) {
			return false; 
		}
		return true;
	}

	/**
	 * 验证用户名是否唯一
	 * @param [type] $username [description]
	 */
	public function IsUniqueUsername($username) {
		$id = $this->where( array('username' => $username) )->getField('id');
		if($id) {
			return false;
		}
		return true;
	}

	/**
	 * 读取配置验证用户名是否被禁止
	 * @param  string $username
	 * @return boolean 
	 */
	protected function checkDenyUserName($username) {
		return true;
	}

	/**
	 * 获取用户状态
	 * @return [type] [description]
	 */
	protected function getStatus() {
		return 1;
	}

}